package com.jinzhi.jzweb.controller;


import java.text.SimpleDateFormat;
import java.util.*;

import com.jinzhi.jzweb.domain.vo.QrUserVO;
import com.jinzhi.jzweb.pojo.ExaminationCostImportVo;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.jzweb.domain.QrUserDO;
import com.jinzhi.jzweb.service.QrUserService;
import com.jinzhi.common.utils.Result;

import javax.servlet.http.HttpServletResponse;

/**
 * 
 * <pre>
 * 拖育园扫码记录表
 * </pre>
 * <small> 2024-10-10 15:05:42 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/qrUser")
public class QrUserController extends BaseController {
	@Autowired
	private QrUserService qrUserService;
	
	@GetMapping()
	@RequiresPermissions("jzweb:qrUser:qrUser")
	String QrUser(){
	    return "jzweb/qrUser/qrUser";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("jzweb:qrUser:qrUser")
	public Result<Page<QrUserDO>> list(QrUserDO qrUserDTO){
		// 查询列表数据
		Page<QrUserDO> page = qrUserService.selectPage(getPage(QrUserDO.class),
					qrUserService.convertToEntityWrapper().orderBy("id", false));
		List list = new ArrayList();
		for (int i = 0; i < page.getRecords().size(); i++) {
				QrUserDO obj = (QrUserDO) page.getRecords().get(i);
			list.add(obj);
		}
		page.setRecords(list);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("jzweb:qrUser:add")
	String add(){
	    return "jzweb/qrUser/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:qrUser:edit")
	String edit(@PathVariable("id") Long id,Model model){
		QrUserDO qrUser = qrUserService.selectById(id);
		model.addAttribute("qrUser", qrUser);
	    return "jzweb/qrUser/edit";
	}
	
	@Log("添加拖育园扫码记录表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:qrUser:add")
	public Result<String> save( QrUserDO qrUser){
		qrUserService.insert(qrUser);
        return Result.ok();
	}
	
	@Log("修改拖育园扫码记录表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:qrUser:edit")
	public Result<String>  update( QrUserDO qrUser){
		boolean update = qrUserService.updateById(qrUser);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除拖育园扫码记录表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:qrUser:remove")
	public Result<String>  remove( Long id){
		qrUserService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除拖育园扫码记录表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:qrUser:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		qrUserService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}



	/**
	 * 导出
	 *
	 * @param response
	 */
	@RequestMapping("/downloadQrUser")
	@ResponseBody
	public void downloadQrUser(HttpServletResponse response) {
		List<QrUserDO> qrUserDOS = qrUserService.selectList(null);
		List<QrUserVO> qrUserVOList =new ArrayList<>();

		for (int i=0;i<qrUserDOS.size();i++){
			QrUserVO qrUserVO=new QrUserVO();
			qrUserVO.setName(qrUserDOS.get(i).getName());
			qrUserVO.setOrgName(qrUserDOS.get(i).getOrgName());
			qrUserVO.setPhone(qrUserDOS.get(i).getPhone());
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
			qrUserVO.setCreateTime(sdf.format(qrUserDOS.get(i).getCreateTime()));
			qrUserVOList.add(qrUserVO);
		}

		//导出操作
		EasyPoiUtil.exportExcel(qrUserVOList, "托育园二维码参观人员信息导出", "托育园二维码参观人员信息导出", QrUserVO.class, "托育园二维码参观人员信息导出", response);
	}

}
